// 1.用生成器替换迭代器
function* foo(arr) {
    // 1.手动迭代
    // for (const item of arr) {
    //     yield item
    // }
    // 2. yield*后跟一个可迭代对象
    yield* arr
}

const names = [111, 222, 333, 444]
const bar = foo(names)
console.log(bar.next());
console.log(bar.next());
console.log(bar.next());
console.log(bar.next());
console.log(bar.next());


function* num(start, end) {
    for (let i = start; i <= end; ++i) {
        yield i
    }
}
const count = num(0, 2)
console.log(count.next());
console.log(count.next());
console.log(count.next());
console.log(count.next());
console.log(count.next());


class Persons {
    constructor(person) {
        this.persons = person
    }
    *[Symbol.iterator]() {
        // yield* this.persons
        for (const p of this.persons) {
            yield p.name
        }
    }

}
const p1 = new Persons([{
    name: '张三',
    age: 111
}, {
    name: '李四',
    age: 222
}, {
    name: '王五',
    age: 333
}])
for (const item of p1) {
    console.log(item);
}